<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\Filesystem;

class Index extends BaseController
{
    public function index()
    {
        $title = '电子商城';
        $login = Session::get('account');
        
        if(!Session::has('account')){
            return redirect('/admin/login');  //没有登录就跳转到登录页面
        }

        $menu = Db::table('shop_menu')->where('fid',0)->select();
        $left = $menu->toArray();
        foreach($left as &$left_v){
            $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
        }
        //print_r($left);

        $param = Request::param();  //取出所有参数
        if(isset($param['keyword'])){   //根据关键字搜索
            $keyword = $param['keyword'];
            //$list = Db::table('shop_goods')->where('title','like',"%$keyword%")->select();
            //$list = Db::table('shop_goods')->whereLike('title',"%$keyword%")->select();
            $list = Db::table('shop_goods')->whereLike('title',"%$keyword%")->paginate(3);  //分页
        }else{      //显示所以数据
            //$list = Db::table('shop_goods')->select();
            $list = Db::table('shop_goods')->paginate(6);   //分页
        }

        //$right = $list->toArray();
        $right = $list;
        foreach($right as &$right_v){
            $right_v['cat'] = Db::table('shop_cat')->where('id',$right_v['cat'])->value('name');
        }
        //print_r($right);
        
        View::assign([
            'title'  => $title,
            'login' => $login,
            'left' => $left,
            'right' => $right,
            'list'  =>$list     //分页数据
        ]);
        return View::fetch();
        
    }
    public function add(){
        if(Request::method() == 'POST'){    //点击保存
            $all = Request::param();
            $all['add_time'] = time();
           
            $file =request()->file('image');
            $savename = Filesystem::disk('public')->putFile( '/', $file);
            $all['image']=$savename; 
            $insert = Db::table('shop_goods')->insert($all);
            if($insert){  //成功
                // echo json_encode(['code'=>0,'msg'=>'添加成功']);
                echo "<script>alert('添加成功');window.location='/admin/index'</script>";
                exit;
            }else{  //失败
                // echo json_encode(['code'=>1,'msg'=>'添加失败']);
                echo "<script>alert('添加失败');window.location='/index/add'</script>";
                exit;
            }
           dump($all);
        }else{  //第一次点击添加时
            $title = '电子商城';
            $login = Session::get('account');
            if(!Session::has('account')){
                return redirect('/admin/login');  //没有登录就跳转到登录页面
            }
            $cat = Db::table('shop_cat')->where('status',1)->select();
            $menu = Db::table('shop_menu')->where('fid',0)->select();
            $left = $menu->toArray();
            foreach($left as &$left_v){
                $left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
            }
            View::assign([
                'title' =>$title,
                'login' =>$login,
                'cat' => $cat,
                'left' =>$left
            ]);
            return View::fetch();
        }
    }
    public function edit(){
        if(Request::method() == 'POST'){   //保存执行修改操作
            $all = Request::param();
            $all['add_time'] = time();
            $update = Db::table('shop_goods')->where('id',$all['id'])->update($all);
            if($update){    //成功
                echo json_encode(['code'=>0,'msg'=>'修改成功']);
                exit;
            }else{  //失败
                echo json_encode(['code'=>1,'msg'=>'修改失败']);
                exit;
            }
        }else{      //查询需要修改记录
            $id = Request::param('id');
            $shop = Db::table('shop_goods')->where('id',$id)->find();
            $cat = Db::table('shop_cat')->where('status',1)->select();
            View::assign([
                'shop' => $shop,
                'cat' => $cat
            ]);
            return View::fetch();
        }
    }
    public function del(){
        $id = Request::param('id');
        $delete = Db::table('shop_goods')->where('id',$id)->delete();
        if($delete){    //成功
            echo json_encode(['code'=>0,'msg'=>'删除成功']);
            exit;
        }else{  //失败
            echo json_encode(['code'=>1,'msg'=>'删除失败']);
            exit;
        }
    }
}